package persistencia;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import Negocio.Facebook;

public class Facebook_DAO {

	public boolean inserir(Facebook face, String chave,String hashtag) {

		Connection conn = null;
		PreparedStatement ps = null;

		conn = ConexaoBanco.connectDB();
		try {
			String sql0 = "INSERT INTO usuario (nome) VALUES(?)";
			ps = conn.prepareStatement(sql0);
	        ps.setString(1, face.getUsuario());
	        ps.execute();

	        String sql1 = "INSERT INTO post (texto,data,hora) VALUES(?,?,?)";
			ps = conn.prepareStatement(sql1);
	        ps.setString(1, face.getTexto());
	        ps.setDate(2, (Date) face.getData());
	        ps.setString(3, face.getHora());
	        ps.execute();
	        
	        String sql2 = "INSERT INTO sentimento (sentimento) VALUES(?)";
			ps = conn.prepareStatement(sql2);
	        ps.setInt(1, face.getSentimento());
	        ps.execute();
	        
	        String sql3 = "INSERT INTO hashtag (hashtag) VALUES(?)";
			ps = conn.prepareStatement(sql3);
	        ps.setString(1, hashtag);
	        ps.execute();
	        
	        String sql4 = "INSERT INTO palavra_chave (palavra_chave) VALUES(?)";
			ps = conn.prepareStatement(sql4);
	        ps.setString(1, chave);
	        ps.execute();
	        
	        String sql5 = "INSERT INTO local (rua,bairro,cidade,pais,estado) VALUES(?,?,?,?,?)";
			ps = conn.prepareStatement(sql5);
	        ps.setString(1, face.getRua());
	        ps.setString(2, face.getBairro());
	        ps.setString(3, face.getCidade());
	        ps.setString(4, face.getPais());
	        ps.setString(5, face.getEstado());
	        ps.execute();
	        
	        return true;

		} catch (SQLException ex) {
			Logger.getLogger(Facebook_DAO.class.getName()).log(Level.SEVERE, null,ex);
			return false;
		} finally {
			close(ps, conn, null);
		}
	}
		
		private void close(PreparedStatement ps, Connection conn, ResultSet rs) {
			RuntimeException r = null;
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException ex) {
					r = new RuntimeException(ex);
					Logger.getLogger(Facebook_DAO.class.getName()).log(Level.SEVERE,
							null, ex);
				}
			}
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException ex) {
					r = new RuntimeException(ex);
					Logger.getLogger(Facebook_DAO.class.getName()).log(Level.SEVERE,
							null, ex);
				}
			}
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException ex) {
					r = new RuntimeException(ex);
					Logger.getLogger(Facebook_DAO.class.getName()).log(Level.SEVERE,
							null, ex);
				}
			}
			if (r != null) {
				throw r;
			}
		}

	}